﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using CPXMenuEditor.Controls;

namespace CPXMenuEditor.Forms
{
    public partial class NewFolder : Form
    {
        string folderName = string.Empty;
        DirectoryNode parentDirectory = null;

        public string FolderName
        {
            get { return folderName; }
        }

        public NewFolder(DirectoryNode parentDirectory)
        {
            InitializeComponent();

            this.parentDirectory = parentDirectory;
        }

        private bool IsValidFolderName(string name)
        {
            char[] invalidChars = Path.GetInvalidFileNameChars();
            foreach (char invalidChar in invalidChars)
                if (name.Contains(new string(invalidChar, 1)))
                    return false;
            return true;
        }

        private bool IsUniqueFolderName(string name)
        {
            if (string.IsNullOrEmpty(name))
                return true;

            foreach (DirectoryNode directoryNode in parentDirectory.DirectoryNodes)
                if (directoryNode.Name.ToLower() == name.ToLower())
                    return false;

            return true;
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (!IsValidFolderName(textBox.Text))
            {
                MessageBox.Show(this, "Folder Name cannot contain the illegal file characters \""
                                    + new string(Path.GetInvalidFileNameChars()) + "\"", MainFrm.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox.Text = folderName;
                return;
            }

            if (!IsUniqueFolderName(textBox.Text))
            {
                MessageBox.Show(this, "A folder already exists with this name", MainFrm.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox.Text = folderName;
                return;
            }

            folderName = textBox.Text;
        }


    }
}
